package ua.org.extjsbuilder.parser.runtime.instructions;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ua.org.extjsbuilder.parser.IInstruction;
import ua.org.extjsbuilder.parser.IScope;
import ua.org.extjsbuilder.parser.JSObject;
import ua.org.extjsbuilder.parser.JSParseException;

public class Condition implements IInstruction {

	public static final String TYPE_IF = "if";
	
	public static final String TYPE_SWITCH = "switch";
	
	private String name;
	
	private IInstruction condition;
	
	private List<IInstruction> mainBlock;
	
	private List<IInstruction> elseBlock;
	
	private Map<JSObject, List<IInstruction>> cases;
	
	/**
	 * IF condition
	 * @param condition
	 * @param mainBlock
	 * @param elseBlock
	 */
	public Condition(IInstruction condition,
			List<IInstruction> mainBlock, 
			List<IInstruction> elseBlock) {
		this.name = TYPE_IF;
		if(condition==null)throw new JSParseException("Bad state - condition is NULL");
		if(mainBlock==null)throw new JSParseException("Bad state - condition is NULL");
		if(elseBlock==null)elseBlock = new LinkedList<IInstruction>();
		this.condition = condition;
		this.mainBlock = mainBlock;
		this.elseBlock = elseBlock;
	}
	/**
	 * SWITCH-CASE condition
	 * @param condition
	 * @param cases
	 */
	public Condition(IInstruction condition,
			Map<JSObject, List<IInstruction>> cases) {
		this.name = TYPE_SWITCH;
		if(condition==null)throw new JSParseException("Bad state - condition is NULL");
		if(cases==null)throw new JSParseException("Bad state - cases is NULL");
		this.cases = cases;
		this.condition = condition;
	}

	public void compile(Writer out, boolean optimize) throws IOException {
		// TODO Auto-generated method stub
		
	}

	public JSObject execute(IScope scope, JSObject[] args) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getName() {
		return name;
	}
	
	public IInstruction getCondition() {
		return condition;
	}
	
	public List<IInstruction> getMainBlock() {
		return Collections.unmodifiableList(mainBlock);
	}
	
	public List<IInstruction> getElseBlock() {
		return Collections.unmodifiableList(elseBlock);
	}
	public Map<JSObject, List<IInstruction>> getCases() {
		return Collections.unmodifiableMap(cases);
	}
}
